package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.http.HttpUtil;
import com.patchmanage.back.entity.InventoryDiscovery;
import com.patchmanage.back.result.CodeMsg;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IInventoryDiscoveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 *  资产发现控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-03-20
 */
@Api(value = " 资产发现 controller", tags = {"资产发现  Api"})
@RestController
@RequestMapping("/back/inventoryDiscovery")
public class InventoryDiscoveryController {

    @Autowired
    public IInventoryDiscoveryService iInventoryDiscoveryService;

    @Value("${ansible.domain}")
    public String ansibleDomain;

    /**
     * 获取获取资产发现设置信息
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取资产发现设置信息接口", notes = "获取资产发现设置信息接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body"),
    })
    public Result<InventoryDiscovery> getById(@ApiIgnore @RequestBody Dict dict) {
        if (iInventoryDiscoveryService.list().size() == 0) {
            return Result.error(CodeMsg.LIST_EMPTY);
        }
        return Result.success(iInventoryDiscoveryService.list().get(0));
    }

    /**
     * 修改或新增资产发现设置信息
     * @param inventoryDiscovery
     * @return
     */
    @ApiOperation(value = "修改或新增资产发现设置信息接口", notes = "修改资产发现设置信息接口")
    @RequestMapping(value = "updateById", method = RequestMethod.POST)
    public Result<Boolean> updateById(@RequestBody InventoryDiscovery inventoryDiscovery) {
        iInventoryDiscoveryService.saveOrUpdate(inventoryDiscovery);
        if(inventoryDiscovery.getId() != null){
            HttpUtil.get("http://" + ansibleDomain + "/web/inventory_discovery");
        }
        return Result.success(true);
    }
}
